package cn.sw.auth.granter;

import cn.sw.auth.context.ApplicationContextHolder;
import cn.sw.common.entity.GrantType;
import lombok.AllArgsConstructor;

import java.util.HashMap;
import java.util.Map;

@AllArgsConstructor
public class UserInfoGranterBuilder {
    private static final Map<GrantType, IUserInfoGranter> GRANTER_POOL = new HashMap<GrantType, IUserInfoGranter>();

    static {
        GRANTER_POOL.put(GrantType.PASSWORD, ApplicationContextHolder.getContext().getBean(PasswordUserInfoGranter.class));
        GRANTER_POOL.put(GrantType.PHONENUM, ApplicationContextHolder.getContext().getBean(PhoneNumUserInfoGranter.class));
    }

    public static IUserInfoGranter getUserInfoGranter(GrantType grantType) throws Exception {
        IUserInfoGranter userInfoGranter = GRANTER_POOL.get(grantType);
        if (userInfoGranter == null) {
            throw new Exception("no grantType was found");
        } else {
            return userInfoGranter;
        }
    }
}
